﻿using System.IO;
using System.Windows.Forms;

namespace ChengDu.Com.WebApiService
{
    public  class AutoStart
    {
        private const string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
        /// <summary>
        /// 设置自己为自启动
        /// </summary>
        public  void SetAutoStart()
        {
            try
            {
                if (!CheckAutoStart())
                {
                    string path = Application.ExecutablePath.ToLower();
                    string name = Path.GetFileName(path);
                    var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
                    if (regKey == null) regKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regPath);
                    regKey.SetValue(name, path);
                }
                else
                { 
                
                }
            }
            catch { }
        }
        /// <summary>
        /// 检查是否已注册
        /// </summary>
        /// <returns></returns>
        public  bool CheckAutoStart()
        {
            var SubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath);
            string name = GetApplicationName();
            var pathO = SubKey.GetValue(name);
            if (pathO == null)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 获取应用名称
        /// </summary>
        /// <returns></returns>
        public static string GetApplicationName()
        {
            string path = Application.ExecutablePath.ToLower();
            return  Path.GetFileName(path);
        }
        /// <summary>
        /// 启动webapi服务
        /// </summary>
        /// <param name="url"></param>
        /// <param name="message"></param>
        /// <returns></returns>

    }
}
